<?php
    /**
    * Table.php
    * 
    * @author Osuvaldo Ramos <xdracox@gmail.com>
    * @copyright Copyright (c) 2008, Osuvaldo Ramos
    * @license http://www.opensource.org/licenses/bsd-license.php
    * @package Atman
    */
    
    /**
    * Base class for classes that represent a database table.
    * @package Atman
    */
    abstract class AT_DB_Table extends AT_Object_DB
    {
        /**
        * The name of the table this class represents.
        * @var string
        */
        protected $tableName;
        
        /**
        * The constructor sets up the actual table name including the table
        * prefix.
        * @param string $tableName The base name of the table.
        */
        public function __construct($tableName)
        {
            parent::__construct();
            
            $this->tableName = AT_TABLE_PREFIX.$tableName;
        }
        
        /**
        * Inserts a new row into the table.
        * @param array $data Associative array with all the field information.
        * @return void
        */
        public function insert($data)
        {
            $cols = '';
            $vals = '';
            $index = 0;
            $lim = sizeof($data);
            
            foreach ( $data as $col => $val )
            {
                if ( is_string($val) )
                {
                    $val = $this->mdb2->quote($val, 'text');
                }
                
                $cols .= $col;
                $vals .= $val;
                
                if ( $index < $lim - 1)
                {
                    $cols .= ',';
                    $vals .= ',';
                }
                
                $index++;
            }
            
            $sql = 'INSERT
                        INTO '.$this->tableName.' ('.$cols.')
                        VALUES ('.$vals.')';
            $result = $this->mdb2->exec($sql);
            if ( PEAR::isError($result) )
            {
                $msg = 'MySQL INSERT failed: '.$sql;
                $this->reportError($msg, PEAR_LOG_ALERT);
            }
        }
        
        /**
        * __destruct()
        */
        public function __destruct()
        {
            parent::__destruct();
        }
    }
?>
